package cc.lucien.servlet;

import cc.lucien.entity.User;
import cc.lucien.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

@MultipartConfig(location = "F:\\photo\\temp")
@WebServlet("/user")
public class UserServlet extends HttpServlet {

    UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method == null) {
            method = "findAll";
        }
        switch (method){
            case "findAll":
                List<User> list = userService.findAll();
                req.setAttribute("list", list);
                req.getRequestDispatcher("admin.jsp").forward(req, resp);
                break;
            case "delete":
                String username = req.getParameter("username");
                userService.deleteByUsername(username);
                resp.sendRedirect("user");
                break;
            case "findByUsername":
                username = req.getParameter("username");
                User user = userService.findByUsername(username);
                req.setAttribute("user", user);
                req.getRequestDispatcher("update.jsp").forward(req, resp);
                break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method){
            case "add":
                String username = req.getParameter("username");
                String password = req.getParameter("password");
                String realname = req.getParameter("realname");
                Part part = req.getPart("img");
                String email = req.getParameter("email");
                String phoneStr = req.getParameter("phone");
                Long phone = Long.parseLong(phoneStr);
                String source = req.getParameter("source");
                userService.add(username,password,realname,part,email,phone,source);
                break;
            case "update":
                username = req.getParameter("username");
                password = req.getParameter("password");
                realname = req.getParameter("realname");
                part = req.getPart("img");
                String currentImg = req.getParameter("currentImg");
                email = req.getParameter("email");
                phoneStr = req.getParameter("phone");
                phone = Long.parseLong(phoneStr);
                source = req.getParameter("source");
                userService.update(username,password,realname,part,currentImg,email,phone,source);
                break;
        }
        //重定向获取数据返回视图
        resp.sendRedirect("user");
    }
}
